<?php
class MCategory extends CI_Model{

	public function __construct()
    {
        parent::__construct();
    }
    
    function getCategoryDropdown()
    {
        $data = array();
		$this->db->order_by("category");
        $this->db->where('activate',1);
        $Q = $this->db->get('category');
        $data[0] = '...Select Category...';
	    if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['category_id']] = $row['category'];
	       }
         }
         $Q->free_result();    
	     return $data; 	
    }
    
    function getCategoryById($id){
	    $data = array();
	    $this->db->where('category_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('category');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
	
	function getCategoryByTitle($title){
	    $data = array();
	    $this->db->where('category',db_clean($title));
	    $this->db->where('activate', 1);
	    $this->db->limit(1);
	    $Q = $this->db->get('category');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
		
	 function getCategoryList(){
	     $data = array();
	     $this->db->order_by("category");
	     $Q = $this->db->get('category');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
     
     function addCategory(){
		$data = array( 
			'category' => db_clean($_POST['category']),
            'remark' => $_POST['remark'],
			'activate' => db_clean(isset($_POST['activate']) ? 1 : 0)
		);
		$this->db->insert('category', $data);	 
	 }
	 
	 function updateCategory(){
		$data = array( 
			'category' => db_clean($_POST['category']),
            'remark' => $_POST['remark'],
			'activate' => db_clean(isset($_POST['activate']) ? 1 : 0)
		);
        $this->db->where('category_id', id_clean($_POST['category_id']));
		$this->db->update('category', $data);	
	 }
	 function inactiveCategory($id){
	 	$data = array('activate' => 0);
	 	$this->db->where('category_id', id_clean($id));
		$this->db->update('category', $data);	
	 }
	 
	 function deleteCategory($id){
	 	$this->db->where('category_id', id_clean($id));
		$this->db->delete('category');	
	 }
 }